import defaultUser from "@/config/BasicUser.ts";
import tokenUtil from "@/utils/TokenUtil.ts";
import {IUserInfoDto} from "@/api/Types/ObjInterfaces.ts";

const tokenKey = "token"
const userInfoKey = "userInfo"

class LocalUtil {
    public saveToken = (token: string): void => {
        window.localStorage.setItem(tokenKey, token)
        const userInfo = tokenUtil.parseToken(token);
        window.localStorage.setItem(userInfoKey, JSON.stringify(userInfo))
    }

    public getToken = (): string => {
        return window.localStorage.getItem(tokenKey);
    }

    public removeToken = () => {
        window.localStorage.removeItem(tokenKey)
        window.localStorage.removeItem(userInfoKey)
    }

    public getUserInfoDto(): IUserInfoDto {
        const item = window.localStorage.getItem(userInfoKey);
        if (item == null || item == "null") {
            return defaultUser
        }
        return JSON.parse(item)
    }
}

export default new LocalUtil()